package demo.controller;

import demo.pojo.Brand;
import demo.pojo.Comm;
import demo.pojo.Page;
import demo.response.Result;
import demo.service.CommService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName 刘星宇
 * @Description TODO
 * @date 2023/4/2 18:12
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/comm")
public class CommController {
    @Resource
    private CommService commService;
    @RequestMapping("getComm")
    public Result getPerm(Integer currentPage, Integer pageSize){
        Page<Comm> p = new Page<Comm>();
        Brand brand=null;
        p.setCurrentPage(currentPage);
        p.setPageSize(pageSize);
        List<Comm> categoryList = commService.getAllByPage(p,new Comm());
        for (Comm comm :categoryList){
            brand=commService.findByIds(comm.getBid());
            comm.setBname(brand.getBname());
        }
        p.setLists(categoryList);
        p.setTotalRecord(commService.getCount(new Comm()));
        return Result.ok(p);
    }
    @RequestMapping("updateCommOrAdd")
    public Result updatePermOrAdd(@RequestBody Comm comm){
        System.err.println(comm.getCid());
        if (comm.getCid()!=null){
            commService.updateComm(comm);
        }else{
            commService.addComm(comm);
        }
        return Result.ok();
    }
    @RequestMapping("delComm")
    public Result delPerm(int cid){
        commService.delComm(cid);
        return Result.ok();
    }
}
